package com.svs.web.controller.project;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.svs.common.utils.ExcelUtil;
import com.svs.framework.shiro.util.ShiroUtils;
import com.svs.project.domain.ProLine;
import com.svs.project.service.ProLineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.svs.common.annotation.Log;
import com.svs.common.enums.BusinessType;
import com.svs.project.domain.ProLineLog;
import com.svs.project.service.ProLineLogService;
import com.svs.framework.web.BaseController;
import com.svs.framework.web.page.TableDataInfo;
import com.svs.common.base.AjaxResult;

/**
 * 每天生产线情况日志 信息操作处理
 * 
 * @author 杜建桥
 * @date 2023-08-12
 */
@Controller
@RequestMapping("/project/proLineLog")
public class ProLineLogController extends BaseController {

    private String prefix = "project/proLineLog";
	
	@Autowired
	private ProLineLogService proLineLogService;
	@Autowired
	private ProLineService proLineService;
	
	@RequiresPermissions("project:proLineLog:view")
	@GetMapping()
	public String proLineLog() {
	    return prefix + "/proLineLogList";
	}
	
	/**
	 * 查询每天生产线情况日志列表
	 */
	@RequiresPermissions("project:proLineLog:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProLineLog proLineLog) {
		startPage();
        List<ProLineLog> list = proLineLogService.selectAllList(proLineLog);
		return getDataTable(list);
	}
	
	/**
	 * 新增每天生产线情况日志
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/addProLineLog";
	}
	
	/**
	 * 新增保存每天生产线情况日志
	 */
	@RequiresPermissions("project:proLineLog:add")
	@Log(title = "每天生产线情况日志", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProLineLog proLineLog) {
		ProLine proLine = proLineService.selectByCode(proLineLog.getLineCode());
		if (proLine == null) {
			return error("没有该生产线");
		}
		if (proLineLog.getNowTime() == null) {
			proLineLog.setNowTime(new Date());
		}
		proLineLog.setLineName(proLine.getLineName());
		proLineLog.setPlanNum(proLine.getPlanNum());
		proLineLog.setRunState(proLine.getRunState());
		proLineLog.setLineId(proLine.getId());
		proLineLog.setCreateBy(ShiroUtils.getLoginName());
		if (proLineLog.getRealNum() != null && proLineLog.getPlanNum() != null && proLineLog.getPlanNum() != 0 && proLineLog.getReachRate() == null) {
			// 先转为浮点数计算，再乘以 100 转换为百分比
			double percentage = (double) proLineLog.getRealNum() / proLineLog.getPlanNum() * 100;
			// 格式化为百分比，保留两位小数，不足补0
			DecimalFormat df = new DecimalFormat("0.00%");
			proLineLog.setReachRate(df.format(percentage / 100));
		}
		return toAjax(proLineLogService.save(proLineLog));
	}

	/**
	 * 修改每天生产线情况日志
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		ProLineLog proLineLog = proLineLogService.selectByPrimaryKey(id);
		mmap.put("proLineLog", proLineLog);
	    return prefix + "/editProLineLog";
	}
	
	/**
	 * 修改保存每天生产线情况日志
	 */
	@RequiresPermissions("project:proLineLog:edit")
	@Log(title = "每天生产线情况日志", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProLineLog proLineLog) {
		proLineLog.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(proLineLogService.update(proLineLog));
	}
	
	/**
	 * 删除每天生产线情况日志
	 */
	@RequiresPermissions("project:proLineLog:remove")
	@Log(title = "每天生产线情况日志", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String[] ids) {		
		return toAjax(proLineLogService.deleteByPrimaryKeys(ids));
	}

	@Log(title = "每天生产线情况日志", businessType = BusinessType.EXPORT)
	@RequiresPermissions("project:proLineLog:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ProLineLog proLineLog) {
		List<ProLineLog> list = proLineLogService.selectAllList(proLineLog);
		ExcelUtil<ProLineLog> util = new ExcelUtil<>(ProLineLog.class);
		return util.exportExcel(list, "proLineLog");
	}
}
